import { _record } from '../../common/parking'
import { formatDate, formatNumber } from '../../utils/util'
const computedBehavior = require('miniprogram-computed')
const app = getApp()
Component({
  behaviors: [computedBehavior],
  data: {
    openid: '',
    tabMenus: [
      {
        name: '最近7天',
        value: 7
      },
      {
        name: '最近15天',
        value: 15
      },
      {
        name: '最近30天',
        value: 30
      }
    ],
    currentIndex: 0,
    list: []
  },
  computed: {
    day () {
      let index = this.data.currentIndex
      return this.data.tabMenus[index].value
    }
  },
  methods: {
    tabChange(e) {
      let currentIndex = parseInt(e.detail)
      this.setData({
        currentIndex
      }, () => {
        this.totalQuery()
      })
    },
    totalQuery () {
      let openid = this.data.openid
      app.loading('加载中')
      _record(openid, this.data.day).then(res => {
        wx.hideLoading()
        if (res.data.IsSuccess) {
          let list = res.data.Data
          // if (list.length > 0) {
          //   wx.setStorageSync('latestParkNum', list[0].carCode.trim())
          // }
          list = res.data.Data.map(item => {
            let code = item.carCode.split('')
            code.splice(2, 0, '·')
            item.carCode = code.join('')
            let intime = item.intime.split('T')
            item.indate = intime[0]
            item.intime = intime[1]
            let paytime = item.paytime.split('T')
            item.paydate = paytime[0]
            item.paytime = paytime[1]
            item.paidmoney = formatNumber(item.paidmoney / 100, 2)
            item.jmmoney = formatNumber(item.jmmoney / 100, 2)
            return item
          })
          this.setData({
            list
          })
        } else {
          wx.showModal({
            title: '对不起',
            content: res.data.Msg,
            showCancel: false
          })
        }
      }).catch(err => {
        console.log(err)
        wx.hideLoading()
        wx.showModal({
          title: '对不起',
          content: '请求失败，请稍后再试',
          showCancel: false
        })
      })
    },
    onLoad(options) { },
    onReady() { },
    onShow() {
      app.memberReadyCb = () => {}
      app.fansReadyCb = () => {
        // app.checkMember()
        this.data.openid = app.globalData.fans.AppOpenID
        this.totalQuery()
      }
      app.init()
    },
    onShareAppMessage() {
      return app.shareInfo
    }
  }
})